package com.luyuan.crm.user.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.model.DataModel;

import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.user.Party;
import com.luyuan.crm.user.service.PartyService;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.web.jsf.model.PageDataModel;
import com.ruijie.web.jsf.model.TableOrder;
import com.ruijie.web.jsf.model.TablePage;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.foundation.query.criteria.RestrictionTemplate;

/**
 * <p>Title: 用户CRUD示例         </p>
 * <p>Description: Function Description </p>
 * <p>Copyright: Copyright (c) 2008     </p>
 * <p>Company: Ruijie Co., Ltd.       </p>
 * <p>Create Time: 2010-8-3           </p>
 * @author Administrator
 * <p>Update Time:                      </p>
 * <p>Updater:                          </p>
 * <p>Update Comments:                  </p>
 */

@Component("partyAction")
@Scope("view")
public class PartyAction extends BaseAction {

    @Resource(name = "partyService")
    private PartyService partyService;

    // 查询条件
    private Party criteria = new Party();

    // 添加或编辑模型
    private Party party;

    public Party getCriteria() {
        return criteria;
    }

    public Party getParty() {
        String userId = this.getRequestParameter("partyId");
        if (party == null && userId != null && !"".equals(userId)) {
            party = partyService.getById(userId);
        }
        if (party == null) {
            party = new Party();
        }
        return party;
    }

    public void setCriteria(Party criteria) {
        this.criteria = criteria;
    }

    public void setParty(Party party) {
        this.party = party;
    }

    public DataModel getTableModel() {
        TablePage limit = TableContext.getTablePage();
        List<TableOrder> tableOrders = TableContext.getTableOrders();
        List<Order> orders = new ArrayList<Order>();
        for (TableOrder order : tableOrders) {
            if (order.toOrder() == null) {
                continue;
            }
            orders.add(order.toOrder());
        }
        orders.add(Order.desc("createTime"));
        CriteriaTemplate c = CriteriaTemplate.forClass(Party.class);
        RestrictionTemplate r = new RestrictionTemplate(true);
        c.add(r.like("userName", criteria.getUserName()));
        c.add(r.like("email", criteria.getEmail()));
        c.add(r.like("telephone", criteria.getTelephone()));
        c.add(r.like("address", criteria.getAddress()));
        Page page = partyService.pagedQuery(c, limit.getPage(), limit.getPageSize(), orders);
        return new PageDataModel(page);
    }

    public void addParty() {
        partyService.addParty(party);

        addGlobalI18nMessage("crud_add_success_msg");
    }

    public void updateParty() {
        partyService.updateParty(party);

        addGlobalI18nMessage("crud_update_success_msg");
    }

    public void removeParty(Party party) {
        partyService.removeParty(party);

        addGlobalI18nMessage("crud_remove_success_msg");
    }

    public void disableParty(Party party) {
        partyService.disableParty(party);

        addGlobalI18nMessage("账号锁定成功！");
    }

    public void enableParty(Party party) {
        partyService.enableParty(party);

        addGlobalI18nMessage("账号解锁成功！");
    }

    public void updatePassword() {
        partyService.updatePassword(party.getAppUser().getUsername(), party.getAppUser().getPassword());

        addGlobalI18nMessage("密码修改成功！");
    }
}
